package com.shm.leetcode;

import com.shm.toOffer.Power;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: shm
 * @dateTime: 2020/9/23 14:43
 * @description: 617. 合并二叉树
 * 给定两个二叉树，想象当你将它们中的一个覆盖到另一个上时，两个二叉树的一些节点便会重叠。
 *
 * 你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠，那么将他们的值相加作为节点合并后的新值，否则不为 NULL 的节点将直接作为新二叉树的节点。
 *
 * 示例 1:
 *
 * 输入:
 * 	Tree 1                     Tree 2
 *           1                         2
 *          / \                       / \
 *         3   2                     1   3
 *        /                           \   \
 *       5                             4   7
 * 输出:
 * 合并后的树:
 * 	     3
 * 	    / \
 * 	   4   5
 * 	  / \   \
 * 	 5   4   7
 * 注意: 合并必须从两个树的根节点开始。
 */
public class MergeTrees {
    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        if (t1==null&&t2==null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode res = new TreeNode();
        queue.offer(t1);
        queue.offer(t2);
        while (!queue.isEmpty()){
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if(poll!=null){
                    res.val +=poll.val;
                }
            }
            queue.offer(t1.left);
            queue.offer(t2.left);
        }
        return res;
    }

    public TreeNode mergeTrees_2(TreeNode t1, TreeNode t2) {
        if (t1==null&&t2==null){
            return null;
        }
        TreeNode res = new TreeNode();
        preMergeTrees(t1,t2,res);
        return res;
    }

    public void preMergeTrees(TreeNode t1, TreeNode t2,TreeNode res){
        if (t1==null&&t2==null){
            return;
        }
        if (t1!=null&&t2!=null){
            res.val=t1.val+t2.val;
            if(t1.left!=null||t2.left!=null) {
                res.left = new TreeNode();
                preMergeTrees(t1.left, t2.left, res.left);
            }
            if(t1.right!=null||t2.right!=null) {
                res.right = new TreeNode();
                preMergeTrees(t1.right, t2.right, res.right);
            }
        }else if (t1!=null){
                res.val=t1.val;
            if(t1.left!=null) {
                res.left = new TreeNode();
                preMergeTrees(t1.left, null, res.left);
            }
            if(t1.right!=null) {
                res.right = new TreeNode();
                preMergeTrees(t1.right, null, res.right);
            }
        }else {
                res.val=t2.val;
            if(t2.left!=null) {
                res.left = new TreeNode();
                preMergeTrees(null, t2.left, res.left);
            }
            if(t2.right!=null) {
                res.right = new TreeNode();
                preMergeTrees(null, t2.right, res.right);
            }
        }
    }


    public TreeNode mergeTrees_3(TreeNode t1, TreeNode t2) {
        if (t1 == null) {
            return t2;
        }
        if (t2 == null) {
            return t1;
        }
        TreeNode merged = new TreeNode(t1.val + t2.val);
        merged.left = mergeTrees_3(t1.left, t2.left);
        merged.right = mergeTrees_3(t1.right, t2.right);
        return merged;
    }
}
